#include<bits/stdc++.h>
using namespace std;
int a[200002],f[200002],ton[200002],p[200002],z[200002],q[200002];
int n,b;
int dfs(int now,int h)
{
    q[now]=max(q[now],h);
    int sum=h;
    if(f[now]!=0)
    {
        if(f[now]==now+1)
        {
            sum=max(sum,dfs(f[now],h+a[now]));
        }
        else
        {
            sum=max(sum,dfs(f[now],z[f[now]]+h+a[now]));
        }
    }
    if(f[now-1]!=0)
    {
        if(f[now-1]!=now)
        {
            sum=max(sum,dfs(f[now-1],z[f[now-1]]+h+a[now-1]));
        }
    }
    return sum;
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(ton,0,sizeof(ton));
        memset(f,0,sizeof(f));
        memset(p,0,sizeof(p));
        memset(q,0,sizeof(q));
        memset(z,0,sizeof(z));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            p[i]=p[i-1];
            scanf("%d",&a[i]);
            if(a[i]==a[i-1])
            {
                p[i]+=a[i];
            }
            if(ton[a[i]]!=0)
            {
                f[ton[a[i]]]=i;
                z[i]=p[i]-p[ton[a[i]]];
            }
            ton[a[i]]=i;
        }
        int ans=0;
        for(int i=1;i<n;i++)
        {
            if(f[i]!=0)
            {
                if(f[i]==i+1)
                {
                    ans=max(ans,dfs(f[i],a[i]+q[i-1]));
                }
                else
                {
                    ans=max(ans,dfs(f[i],a[i]+z[f[i]]+q[i-1]));
                }
            }
            q[i]=max(q[i],q[i-1]);
        }
        printf("%d\n",ans);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
